package com.yanq.liancar.fragment;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/10/31  11:01
 *  @描述       ${TODO}
 */


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.commonsdk.debug.D;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.RescueOrderDetailsActivity;
import com.yanq.liancar.adapter.RescueOrderAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.CouponBean;
import com.yanq.liancar.bean.DaijinQuanBean;
import com.yanq.liancar.bean.RescueOrderBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.ToastUtil;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

public class BRescueOrderFragment extends BaseLYFragment {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_noMessage)
    LinearLayout llNoMessage;
    Unbinder unbinder;

    private int page = 1;
    private boolean isClear = true;
    private boolean isLastPage;
    private RescueOrderAdapter mAdapter;
    private List<RescueOrderBean.DataBean.ListBean> mBeanList = new ArrayList<>();
    private int type ,shopId;

    public static BRescueOrderFragment newInstance(int type,int shopId) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("shopId", shopId);
        BRescueOrderFragment bRescueOrderFragment = new BRescueOrderFragment();
        bRescueOrderFragment.setArguments(bundle);
        return bRescueOrderFragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        Utils.setStatusBar(mActivity, true, false);
        setContentView(R.layout.fragment_brescueorder);
        type=getArguments().getInt("type");
        shopId=getArguments().getInt("shopId");
        initRecyclerview();
        initListener();
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        isClear = true;
        initOkGoWhole();
    }

    private void initRecyclerview() {
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new RescueOrderAdapter(R.layout.item_rescueorder, mBeanList);
        recyclerview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(mActivity, RescueOrderDetailsActivity.class);
                intent.putExtra("id",mBeanList.get(position).getId());
                intent.putExtra("shopID",shopId);
                startActivity(intent);
                mActivity.finish();
            }
        });
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;

                initOkGoWhole();
                refreshlayout.finishRefresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                if (!isLastPage){
                    initOkGoWhole();
                }else {
                    ToastUtil.show(mActivity, "暂无更多数据");
                }
                refreshLayout.finishLoadMore();
            }

        });
    }

    //全部
    private void initOkGoWhole() {
        LogUtils.e("111111111111111",isClear+"");
        OkGo.<String>post(Config.s + "llc/api/RoadRescueController/selectRescueOrderStatus")
                .tag(this)
                .params("shopid", shopId)
                .params("status", type)
                .params("pageNum", page)
                .params("pageSize", 7)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        RescueOrderBean bean = gson.fromJson(response.body(), RescueOrderBean.class);
                        if (bean.isIsSuccess()) {
                            if (bean.getCode()==200){
                                if (isClear) {
                                    mBeanList.clear();
                                }
                                mBeanList.addAll(bean.getData().getList());
                                mAdapter.setNewData(mBeanList);
                                isLastPage=bean.getData().isIsLastPage();
                            }else {
                                RxToast.info(bean.getOutputMessage());
                            }
//                            if (mDList.size() < 1) {
//                                ll_noMessage.setVisibility(View.VISIBLE);
//                            } else {
//                                ll_noMessage.setVisibility(View.GONE);
//                            }
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        RxToast.error("服务器连接超时");
                        super.onError(response);
                    }
                });
    }

}
